//
//  WSQuestionListViewController.m
//  WisdomStar
//
//  Created by 谭飞 on 2018/10/31.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "WSQuestionListViewController.h"
#import "WSAnswerViewController.h"

#import "WSAnswerTableCell.h"
#import "WSAnswerSectionHeader.h"

@interface WSQuestionListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIButton *rightNavButton;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation WSQuestionListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // registe cell
    [self.tableView registerNib:[UINib nibWithNibName:[WSAnswerTableCell className] bundle:nil] forCellReuseIdentifier:[WSAnswerTableCell className]];
    
    // regist section header
    [self.tableView registerNib:[UINib nibWithNibName:[WSAnswerSectionHeader className] bundle:nil] forHeaderFooterViewReuseIdentifier:[WSAnswerSectionHeader className]];
    
    // add nav button
    [self addRightNavButton];
}

// add nav button
- (void)addRightNavButton {
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightNavButton];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
}


// ask question
- (void)askAction {
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return section == 0 ? 2 : 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WSAnswerTableCell *cell = [tableView dequeueReusableCellWithIdentifier:[WSAnswerTableCell className] forIndexPath:indexPath];
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    WSAnswerSectionHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:[WSAnswerSectionHeader className]];
    header.titleLabel.text = section == 0 ? @"今日发布" : @"历史发布";
    return header;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44.f;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section {
    return 0.0000001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}


#pragma mark - Getter

- (UIButton *)rightNavButton {
    if (_rightNavButton == nil) {
        _rightNavButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_rightNavButton setTitle:@"提问" forState:UIControlStateNormal];
        [_rightNavButton setTitleColor:COLOR_HEX(0xE5FFFB) forState:UIControlStateNormal];
        [_rightNavButton.titleLabel setFont:WSFontMake(15)];
        _rightNavButton.xmg_height = kNavBarHeight;
        [_rightNavButton addTarget:self action:@selector(askAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightNavButton;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
